var qiniu = require("qiniu");
//需要填写你的 Access Key 和 Secret Key
qiniu.conf.ACCESS_KEY = 'r8eR81kBwjteO4dWHCXDQafofk9KbtH65xQGZsBk';
qiniu.conf.SECRET_KEY = 'av9H_BxKDAedyKzob7ZOr_9MdC6iBQ7UVGPmknfE';
//要上传的空间
bucket = 'xhkcwechat';
//上传到七牛后保存的文件名
// key = 'my-nodejs-logo.png';

// 生成上传文件的 token
const token = (bucket, key) => {
    const policy = new qiniu_sdk.rs.PutPolicy({isPrefixalScope: 1, scope: bucket + ':' + key})
    return policy.uploadToken()
}

//要上传文件的本地路径
// filePath = './images/3.jpg'
//构造上传函数
var uploadFile = function (uptoken, key, localFile) {
    var extra = new qiniu.io.PutExtra();
    qiniu.io.putFile(uptoken, key, localFile, extra, function (err, ret) {
        if (!err) {
            // 上传成功， 处理返回值
            console.log(ret.hash, ret.key, ret.persistentId);
        } else {
            // 上传失败， 处理返回代码
            console.log(err);
        }
    });
}
//调用uploadFile上传
// uploadFile(token, key, filePath);
module.exports = {uploadFile}
